package com.example.kemusan.service.impl;

import com.example.kemusan.mapper.ResultInfoMapper;
import com.example.kemusan.mapper.StuInfoMapper;
import com.example.kemusan.pojo.Result;
import com.example.kemusan.pojo.ResultInfo;
import com.example.kemusan.pojo.StuInfo;
import com.example.kemusan.service.StuInfoService;
import com.example.kemusan.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.util.Date;
import java.util.List;

@Service
public class StuInfoServiceImpl implements StuInfoService {

    @Autowired
    private StuInfoMapper stuInfoMapper;

    @Autowired
    private ResultInfoMapper resultInfoMapper;

    @Autowired
    public StringUtils stringUtils;

    /**
     * 查询所有考生
     *
     * @return
     */
    @Override
    public Result selectAllStu() {
        List<StuInfo> stuInfos = stuInfoMapper.selectAll();
        return Result.success(stuInfos);
    }

    /**
     * 上传成绩
     *
     * @param resultInfo
     * @return
     */
    @Override
    public Result insertStuScore(ResultInfo resultInfo) {
        if (!(stringUtils.checkString(resultInfo.getIdCard())
                && (resultInfo.getScore() != null && resultInfo.getScore() <= 100))
        )
            return Result.error("参数错误，上传成绩失败！");

        if (resultInfo.getScore() < 90) {
            if (stuInfoMapper.setStatus(resultInfo.getIdCard()) <= 0)
                return Result.error("上传成绩失败!");
        } else {
            if (stuInfoMapper.clearStatus(resultInfo.getIdCard()) <= 0) {
                return Result.error("上传成绩失败!");
            }
        }

        resultInfo.setFinishTime(new Timestamp(new Date().getTime()));
        if (resultInfoMapper.insertResultInfo(resultInfo) > 0)
            return Result.success("上传成绩成功！");
        return Result.error("上传成绩失败！");
    }

    /**
     * 查询成绩
     *
     * @param stuInfo
     * @return
     */
    @Override
    public Result selectScore(StuInfo stuInfo) {
        List<ResultInfo> resultInfos = stuInfoMapper.selectScoreByIdCard(stuInfo);
        if (resultInfos == null || resultInfos.size() == 0)
            return Result.error("未查到该考生的成绩！");

        return Result.success(resultInfos.get(0));
    }

    /**
     * 添加考生
     *
     * @param stuInfo
     * @return
     */
    @Override
    public Result insertStu(StuInfo stuInfo) {
        if (!(stringUtils.checkString(stuInfo.getUsername())
                && stringUtils.checkString(stuInfo.getCarType())
                && stringUtils.checkString(stuInfo.getIdCard())
                && stringUtils.checkString(stuInfo.getImage()))
        )
            return Result.error("用户名、车辆类型、身份证号、证件图片不能为空！");

        StuInfo stu = stuInfoMapper.selectStuByIdCardOrAdmission(stuInfo);
        if (stu != null) {
            if (stuInfoMapper.resetStatus(stuInfo).equals(0))
                return Result.error("添加失败！");
            return Result.success("添加成功！");
        }

        stuInfo.setAdmission((new Date().getTime() % 10000000000L) + "");
        if (stuInfoMapper.insertStu(stuInfo).equals(0))
            return Result.error("添加失败！");
        return Result.success("添加成功！");
    }


}
